package com.tros.general.authentication;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.tros.admin.validation.DecryptValue;

import java.sql.*;

/**
 * Servlet implementation class LoginValidation
 */
@WebServlet("/LoginValidation")
public class LoginValidation extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public LoginValidation() {
        super();
        // TODO Auto-generated constructor stub
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		System.out.println("came here -----------> 2");
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		
		Connection connect =null;
		
		try{
		       Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
		       connect =DriverManager.getConnection("jdbc:odbc:bank","root", " ");
	           System.out.println("DB Connection created :"+connect);
		}
		catch(Exception e)
		{ 
	       	e.printStackTrace();
	       	response.sendRedirect("loginfailure.jsp"); 
		}		
		
		
		try
		{	    
		     String User = request.getParameter("custid");
		     String Pass = request.getParameter("pass");
		     
		     if(User.equalsIgnoreCase("admin"))
		     {
		    	 String command="SELECT * FROM ADMIN WHERE USERNAME='"+User+"' AND PASSWORD='"+Pass+"'";
				 Statement consta=connect.createStatement();
			     ResultSet result=consta.executeQuery(command);

				if(result.next()) 
				{
					 HttpSession session = request.getSession(true);	    
			         session.setAttribute("currentUser",User); 
			         response.sendRedirect("adminHome.jsp");    
			     }else
			     {
			    	 response.sendRedirect("loginfailure.jsp"); 
			     }
				 consta.close();
				 connect.close();
				 
		     }else{
		    	 System.out.println("++++++++++++++++ usernae " +User);
		    	 String command="SELECT * FROM EXISTUSERS WHERE CUSTOMERID='"+User+"'";
				 Statement consta=connect.createStatement();
			     ResultSet result=consta.executeQuery(command);

				if(result.next()) 
				{
					String encryPass=result.getString("PASSWORD");
					
					DecryptValue cryptValue = new DecryptValue(encryPass);
					String Decry = cryptValue.getDecrptValue();
					
					if(Pass.equals(Decry))
					{
						 HttpSession session = request.getSession(true);	
						 String LastName=result.getString("LASTNAME");
				         session.setAttribute("currentUser",LastName); 
				         session.setAttribute("currentID",User); 
				         response.sendRedirect("custHome.jsp");  
					}else
					{
						response.sendRedirect("loginfailure.jsp"); 
					}
					   
			     }else
			     {
			    	 response.sendRedirect("loginfailure.jsp"); 
			     }
				 consta.close();
				 connect.close();
		     }		     
		     
		}
				
		catch (Throwable exception) 	    
		{
		     System.out.println("-------------->  Error "+exception); 
		     response.sendRedirect("loginfailure.jsp"); 
		}
       }
}
